package com.egao.oa.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.oa.biz.BugsBiz;
import com.egao.oa.entity.Bugs;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2020-08-14 16:51:05
 * @version ：1.0
 */
@Repository
public class BugsBizImpl extends BaseDao implements BugsBiz {

	@Override
    public JsonResult list(PageResult2<T> page, Bugs bugs, User loginUser) throws Exception {
        Map<String, String> map  = new HashMap<String, String>();
        RowMapper<Bugs> rm = new BeanPropertyRowMapper<Bugs>(Bugs.class);
        StringBuffer sql = new StringBuffer("select b.id,b.organization_id,b.projects");
        sql.append(",dd.dict_data_name projectsName");
        sql.append(",ddd.dict_data_name stateName");
        sql.append(",b.content,b.modifer,b.checkedperson,b.type");
		sql.append(",b.priority,b.state,b.remark,b.create_time");
		sql.append(",b.create_user,b.update_time,b.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
        sql.append(" from oa_bugs b ");
        sql.append(" inner join sys_user suser on suser.user_id=b.create_user ");
        sql.append(" left join sys_dictionary_data dd on dd.dict_data_code=b.projects and dd.dict_id=3 ");
        sql.append(" left join sys_dictionary_data ddd on ddd.dict_data_code=b.state and ddd.dict_id=4 ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=b.organization_id ");
        sql.append(" where b.deleted=0 and sogzn.deleted=0 ");
        if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and b.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }

        if(Utils.isNotEmpty(bugs)){
			if(Utils.isNotEmpty(bugs.getContent())){
				sql.append(" and instr(b.content,:content)>0 ");
				map.put("content", bugs.getContent());
			}
			if(Utils.isNotEmpty(bugs.getModifer())){
				sql.append(" and instr(b.modifer,:modifer)>0 ");
				map.put("modifer", bugs.getModifer());
			}
			if(Utils.isNotEmpty(bugs.getCheckedperson())){
				sql.append(" and instr(b.checkedperson,:checkedperson)>0 ");
				map.put("checkedperson", bugs.getCheckedperson());
			}
			if(Utils.isNotEmpty(bugs.getType())){
				sql.append(" and instr(b.type,:type)>0 ");
				map.put("type", bugs.getType());
			}
			if(Utils.isNotEmpty(bugs.getPriority())){
				sql.append(" and instr(b.priority,:priority)>0 ");
				map.put("priority", bugs.getPriority()+"");
			}
			if(Utils.isNotEmpty(bugs.getState())){
				sql.append(" and instr(b.state,:state)>0 ");
				map.put("state", bugs.getState());
			}
            if(Utils.isNotEmpty(bugs.getSort())&&Utils.isNotEmpty(bugs.getOrder())){
                sql.append(" order by b."+bugs.getSort()+" "+bugs.getOrder());
            } else {
                sql.append(" order by b.state asc,b.priority desc, b.create_time desc");
            }
        }
        return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
    }

    @Override
    public List<Bugs> list(Bugs bugs, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        RowMapper<Bugs> rm = new BeanPropertyRowMapper<Bugs>(Bugs.class);
        StringBuffer sql = new StringBuffer("select b.id,b.organization_id,b.projects");
        sql.append(",dd.dict_data_name projectsName");
        sql.append(",b.content,b.modifer,b.checkedperson,b.type");
		sql.append(",b.priority,b.state,b.remark,b.create_time");
		sql.append(",b.create_user,b.update_time,b.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
        sql.append(" from oa_bugs b ");
        sql.append(" inner join sys_user suser on suser.user_id=b.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=b.organization_id ");
        sql.append(" where b.deleted=0 and sogzn.deleted=0 ");
        if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and b.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if(Utils.isNotEmpty(bugs)){
//			if(Utils.isNotEmpty(bugs.getName())){
//				sql.append(" and instr(b.name,:name)>0 ");
//				map.put("name", bugs.getName());
//			}
            if(Utils.isNotEmpty(bugs.getSort())&&Utils.isNotEmpty(bugs.getOrder())){
                sql.append(" order by b."+bugs.getSort()+" "+bugs.getOrder());
            } else {
                sql.append(" order by b.state asc,b.priority desc, b.create_time desc");
            }
        }
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(),map, rm);
    }

    @Override
    public Bugs getById(Bugs bugs) {
        RowMapper<Bugs> rm = new BeanPropertyRowMapper<Bugs>(Bugs.class);
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("select b.id,b.organization_id,b.projects");
        sql.append(",dd.dict_data_name projectsName");
        sql.append(",b.content,b.modifer,b.checkedperson,b.type");
		sql.append(",b.priority,b.state,b.remark,b.create_time");
		sql.append(",b.create_user,b.update_time,b.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
        sql.append(" from oa_bugs b ");
        sql.append(" inner join sys_user suser on suser.user_id=b.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=b.organization_id ");
        sql.append(" where b.deleted=0 and sogzn.deleted=0 and b.id=:id ");
        map.put("id", bugs.getId());
        getMapParameter(sql, map);
        return jdbcTemplate.queryForObject(sql.toString(), map, rm);
    }

    @Override
    public boolean save(Bugs bugs, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql =new StringBuffer("insert into oa_bugs (");
        sql.append("organization_id,projects,content,modifer");
		sql.append(",checkedperson,type,priority,state");
		sql.append(",remark,create_user,create_time) values ( ");
        sql.append(":organization_id,:projects,:content,:modifer");
		sql.append(",:checkedperson,:type,:priority,:state");
		sql.append(",:remark,:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("projects", bugs.getProjects());
        map.put("content", bugs.getContent());
        map.put("modifer", bugs.getModifer());
        map.put("checkedperson", bugs.getCheckedperson());
        map.put("type", bugs.getType());
        map.put("priority", bugs.getPriority()==null?null:bugs.getPriority()+"");
        map.put("state", bugs.getState());
        map.put("remark", bugs.getRemark());
        map.put("create_user", loginUser.getUserId() + "");
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean update(Bugs bugs, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql =new StringBuffer("update oa_bugs set ");
        sql.append("projects=:projects,content=:content,modifer=:modifer ");
		sql.append(",checkedperson=:checkedperson,type=:type,priority=:priority ");
		sql.append(",state=:state,remark=:remark,update_time=now() ");
		sql.append(",update_user=:update_user where id=:id ");
        map.put("projects", bugs.getProjects());
        map.put("content", bugs.getContent());
        map.put("modifer", bugs.getModifer());
        map.put("checkedperson", bugs.getCheckedperson());
        map.put("type", bugs.getType());
        map.put("priority", bugs.getPriority()==null?null:bugs.getPriority()+"");
        map.put("state", bugs.getState());
        map.put("remark", bugs.getRemark());
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", bugs.getId());
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean remove(Bugs bugs, User loginUser) {
        int count = 0;
        Map<String, String> map = null;
        if(bugs.getId().length()>0){
            for(String ID : bugs.getId().split(",")){
                map  = new HashMap<String, String>();
                if(ID==null || ID.equals("")){ break; }
                StringBuffer sql =new StringBuffer("update oa_bugs set deleted=1" +
                        ",update_user=:update_user, update_time=now() "
                        + " where id =:id ");
                map.put("update_user", loginUser.getUserId() + "");
                map.put("id", ID);
                getMapParameter(sql, map);
                count = jdbcTemplate.update(sql.toString(),map);
            }
        }
        return count > 0;
    }
}